﻿import QtQuick 2.3
import QtQuick.Controls 1.2
import QmlObjectTest 1.0
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Private 1.0
import QtMultimedia 5.5

import MyCommon 1.0

ApplicationWindow {
    id: window
    visible: true
    width: Screen.width
    height: Screen.height
    title: qsTr("贪吃蛇VS萌梦")

    property real value: 0
    property int result: -1
    property bool bStartGame: false
    property real step: 100

    property real score: 0

    property real speed: 300

    property real lastSpeed: 300

    property bool isFired: false

    property int touchYaoGan: 0     //0=没有，1=触控点1占用，2=触控点2占用
    property int touchSpeed: 0    //0=没有，1=触控点1占用，2=触控点2占用

    property real maxScore: 0

    property bool longPressed: false

    Component.onCompleted: {
        maxScore = common.getScore()
    }

    onMaxScoreChanged: {
        common.saveScore(maxScore)
    }

    onIsFiredChanged: {
        if(isFired){
            lastSpeed = speed
            speed = 100
        }
        else{
            speed = lastSpeed
        }
    }

    onScoreChanged: {

        //每10分提升一个level的速度(-10)
        //level最大20，对应分数200
        if(isFired) return

        var level = Math.floor(score/10)

        if(level==0){
        }
        else{
            if(speed - 10*level <= 100){
                speed = 100
            }
            else{
                speed = 300-10*level
            }
        }

        lastSpeed = speed

        maxScore = Math.max(maxScore, score)
    }

    //减分
    function minusScore(){
        if(score>0){
            score--

            if(snake.items.length>0){
                var obj = snake.items[snake.items.length-1]
                obj.destroy()
                snake.items.splice(snake.items.length-1, 1)
            }
        }
    }

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }

    MyCommon{
        id: common
    }

    Rectangle{
        id: rectBgMain
        anchors.fill: parent
        color: Qt.rgba(219/255, 199/255, 129/255, 1)

        focus: true

        Keys.onBackPressed: {
            options.focus = true
            options.scale = 2
            bStartGame = false
        }
    }

    //虚拟摇杆
    Snaker{
        id: snaker
        x: 150
        y: Screen.height - height - 150
        z: 100
        onSendAngle: {
//            console.log("================", angle)
            snake.setHeadAngle(angle)
        }
        onStop: {
            snake.stopTouch()
        }
    }

    //加速键
    Fire{
        id: fire
        x: Screen.width - width - 150
        y: Screen.height - height - 150
        z: 100
        onPressFire: {
            isFired = false
        }
    }

    //球
    Image{
        id: ball
        source: "qrc:/img/img/ballDis.png"
        y: (parent.height-height)/2
        x: (parent.width-width)-200
        z: 1
    }

    //贪吃蛇
    Snake{
        id: snake        
        z: 1
    }

    //Text
    Label{
        id: textScore
        text: "分数: " + score
        font.pointSize: 30
        font.family: "微软雅黑"
        x: (parent.width-width)/2
        y: 10
        z: 11
        color: "white"        
    }


    //多点触控
    MultiPointTouchArea{
        id: touchArea
        anchors.fill: parent
        mouseEnabled: false
        maximumTouchPoints: 2
        minimumTouchPoints: 1
        z: 100

        touchPoints: [
            TouchPoint{
                id: point1
            },
            TouchPoint{
                id: point2
            }
        ]

        onPressed: {
            if(point1.pressed == true &&
                    (point1.x>=snaker.x && point1.x<=snaker.x+snaker.width
                     && point1.y>=snaker.y && point1.y<=snaker.y+snaker.height)){
                //touch 虚拟摇杆
                if(touchYaoGan == 0){
                    touchYaoGan = 1
                    snaker.snakerSnaker.x = (point1.x-snaker.x-snaker.snakerSnaker.width/2)
                    snaker.snakerSnaker.y = (point1.y-snaker.y-snaker.snakerSnaker.height/2)
                }
            }

            if(point2.pressed == true &&
                    (point2.x>=snaker.x && point2.x<=snaker.x+snaker.width
                     && point2.y>=snaker.y && point2.y<=snaker.y+snaker.height)){
                //touch 虚拟摇杆
                if(touchYaoGan == 0){
                    touchYaoGan = 2
                    snaker.snakerSnaker.x = (point2.x-snaker.x-snaker.snakerSnaker.width/2)
                    snaker.snakerSnaker.y = (point2.y-snaker.y-snaker.snakerSnaker.height/2)
                }
            }

            if(point1.pressed == true &&
                    (point1.x>=fire.x && point1.x<=fire.x+fire.width
                     && point1.y>=fire.y && point1.y<=fire.y+fire.height)){

                //按下加速
                if(touchSpeed == 0){
                    touchSpeed = 1
//                    fire.scale = 0.9
//                    isFired = true

                    timerLongPress.restart()
                }
            }

            if(point2.pressed == true &&
                    (point2.x>=fire.x && point2.x<=fire.x+fire.width
                     && point2.y>=fire.y && point2.y<=fire.y+fire.height)){

                //按下加速
                if(touchSpeed == 0){
                    touchSpeed = 2
//                    fire.scale = 0.9
//                    isFired = true

                    timerLongPress.restart()
                }
            }
        }
        onReleased: {
            if(touchYaoGan == 1 && !point1.pressed){
                touchYaoGan = 0

                snaker.snakerSnaker.x = (snaker.width-snaker.snakerSnaker.width)/2
                snaker.snakerSnaker.y = (snaker.height-snaker.snakerSnaker.height)/2
            }
            else if(touchYaoGan == 2 && !point2.pressed){
                touchYaoGan = 0

                snaker.snakerSnaker.x = (snaker.width-snaker.snakerSnaker.width)/2
                snaker.snakerSnaker.y = (snaker.height-snaker.snakerSnaker.height)/2
            }

            if(touchSpeed == 1 && !point1.pressed){
                touchSpeed = 0
                timerLongPress.stop()
                longPressed = false

                fire.scale = 1
                isFired = false

            }
            else if(touchSpeed == 2 && !point2.pressed){
                touchSpeed = 0

                timerLongPress.stop()
                longPressed = false

                fire.scale = 1
                isFired = false
            }

        }
        onUpdated: {

            if(touchYaoGan == 1 && point1.pressed){
                //点1占用摇杆

                var centerX = snaker.width/2
                var centerY = snaker.height/2

                //snaker.snakerSnaker

                var snakerCX = point1.x - snaker.x
                var snakerCY = point1.y - snaker.y

                //处理边界
                if(snakerCX<snaker.snakerSnaker.width/2){
                    snakerCX = snaker.snakerSnaker.width/2
                }
                else if(snakerCX>snaker.width-snaker.snakerSnaker.width/2){
                    snakerCX = snaker.width-snaker.snakerSnaker.width/2
                }

                if(snakerCY<snaker.snakerSnaker.height/2){
                    snakerCY = snaker.snakerSnaker.height/2
                }
                else if(snakerCY>snaker.height-snaker.snakerSnaker.height/2){
                    snakerCY = snaker.height-snaker.snakerSnaker.height/2
                }

                //移动摇杆
                snaker.snakerSnaker.x = snakerCX - snaker.snakerSnaker.width/2
                snaker.snakerSnaker.y = snakerCY - snaker.snakerSnaker.height/2

                var radian = Math.atan2(snakerCY - centerY, snakerCX - centerX )

                var angle = 180 / Math.PI * radian

                snake.setHeadAngle(angle)
            }
            else if(touchYaoGan == 2 && point2.pressed){
                //点1占用摇杆

                var centerX = snaker.width/2
                var centerY = snaker.height/2

                //snaker.snakerSnaker

                var snakerCX = point2.x - snaker.x
                var snakerCY = point2.y - snaker.y

                //处理边界
                if(snakerCX<snaker.snakerSnaker.width/2){
                    snakerCX = snaker.snakerSnaker.width/2
                }
                else if(snakerCX>snaker.width-snaker.snakerSnaker.width/2){
                    snakerCX = snaker.width-snaker.snakerSnaker.width/2
                }

                if(snakerCY<snaker.snakerSnaker.height/2){
                    snakerCY = snaker.snakerSnaker.height/2
                }
                else if(snakerCY>snaker.height-snaker.snakerSnaker.height/2){
                    snakerCY = snaker.height-snaker.snakerSnaker.height/2
                }

                //移动摇杆
                snaker.snakerSnaker.x = snakerCX - snaker.snakerSnaker.width/2
                snaker.snakerSnaker.y = snakerCY - snaker.snakerSnaker.height/2

                var radian = Math.atan2(snakerCY - centerY, snakerCX - centerX )

                var angle = 180 / Math.PI * radian

                snake.setHeadAngle(angle)
            }
        }
    }

    Button{
        text: "提示"
        anchors.right: parent.right
        anchors.rightMargin: 5
        y: 5
        z: 100
        style: ButtonStyle{}
        onClicked: {
            messageDialog.visible = true
        }
    }

    //options
    MouseArea{
        id: optionsMouse
        anchors.fill: parent
        enabled: options.scale==2?true:false
        z: 100

        Options{
            id: options
            anchors.centerIn: parent
        }
    }

    //start
    StartScreen{
        id: start
        anchors.fill: parent
        z: 101
    }

    MessageDialog{
        id: messageDialog
        icon: StandardIcon.Information
        informativeText: "摇杆移动贪吃蛇\n加速键按住加速！"
    }

    SoundEffect {
        id: playSound
        source: "qrc:/music/music/eat1.wav"
    }


    //长压判断
    Timer{
        id: timerLongPress
        interval: 300
        onTriggered: {
            //加速键被长压
            fire.scale = 0.9
            isFired = true
            longPressed = false
        }
    }

    Timer{
        id: timerGame
        interval: speed
        repeat: true
        running: bStartGame
        onTriggered: {
            if(!snake.isEat)
                snake.move(step)

            if(ball.visible){
                if(checkIsEatBall()){
                    playSound.play()
                    ball.visible = false
                    Math.seed = getSeed()

                    ball.x = Math.floor(Math.random()*(Screen.width-snake.ballRadius*2))
                    ball.y = Math.floor(Math.random()*(Screen.height-snake.ballRadius*2))

                    ball.visible = true

                    snake.eat(ball.source)

                    score++
                }
            }
        }
    }

    function checkIsEatBall(){

        if(ball.visible){
            var sCenterX = snake.head.x + snake.head.width/2
            var sCenterY = snake.head.y + snake.head.height/2

            var bCenterX = ball.x + ball.width/2
            var bCenterY = ball.y + ball.height/2

            var dt = Math.sqrt((sCenterX-bCenterX)*(sCenterX-bCenterX) + (sCenterY-bCenterY)*(sCenterY-bCenterY))

            if(dt <= snake.head.width/2 + ball.width/2){
                return true
            }
        }
        return false
    }
}

